//
//  MBBarSearchView.m
//  房源详情-小区详情
//
//  Created by MB__Lee on 2018/5/29.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBBarSearchView.h"
#import "MBSearchField.h"

@interface MBBarSearchView()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *resetBTN; // 收起按钮

@property (weak, nonatomic) IBOutlet MBSearchField *SearField; //搜索输入框


@property (weak, nonatomic) IBOutlet UILabel *btnLabel;//点击btn 之后，将此label放到上面



@end

static NSString *MBBarSearchView_Identi = @"MBBarSearchView";

@implementation MBBarSearchView

-(void)awakeFromNib{
    [super awakeFromNib];
    self.backgroundColor = [UIColor clearColor];
    
    //刚出现的时候--显示 收起按钮为“放大镜” ，隐藏搜索输入框
//    [self.resetBTN setImage:[UIImage imageNamed:@"fangdajing_icon"] forState:UIControlStateNormal];
    [self.resetBTN setTitle:@"收起" forState:UIControlStateSelected];
    self.resetBTN.backgroundColor = MB_RGBA(247,247, 247, 1.0);
    
    
    self.SearField.hidden = YES;
    self.SearField.placeholder = @"请输入搜索关键字";
    
    
    //设置代理
    self.SearField.delegate = self;
    
    //btnlabel 设置
    _btnLabel.text = @"收起";
    _btnLabel.font =  MBFont(17);
    _btnLabel.backgroundColor = MB_RGBA(247, 247, 247, 1.0);
    _btnLabel.textAlignment = NSTextAlignmentCenter;
    _btnLabel.textColor = MB_mainColor;
    _btnLabel.hidden = YES;
    
}

+(instancetype)MBBarSearchViewCreat{
    MBBarSearchView *barSearch = [[NSBundle mainBundle] loadNibNamed:MBBarSearchView_Identi owner:nil options:nil].lastObject;
    return  barSearch;
}

//点击收起按钮
- (IBAction)shouqiAction:(id)sender {
    UIButton *btn = (UIButton *)sender;
    
    btn.selected = !btn.selected;
    if (!btn.selected) {
        self.SearField.hidden = YES;
        [btn setImage:[UIImage imageNamed:@"fangdajing_icon"] forState:UIControlStateNormal];
        _btnLabel.hidden = YES;
        
        
        // 搜索内容相关 --- 当点击收起，恢复到筛选条件搜索，同时 将键盘弹下
        if ([self.mbBarDelegate respondsToSelector:@selector(MBBarSearchViewSendSearchText:)]) {
            [self.mbBarDelegate MBBarSearchViewSendSearchText:@""];
            [self.SearField resignFirstResponder];
            
        }
        
    }else{
        self.SearField.hidden = NO;

        _btnLabel.hidden = NO;
        
    }
    
    
}
// 点击导航栏的返回按钮的时候，让此方法触发
- (IBAction)navigationBack:(id)sender {
    
    if ([self.mbBarDelegate respondsToSelector:@selector(MBBarSearchViewBack)]) {
        [self.mbBarDelegate MBBarSearchViewBack];
    }
    
    
}

#pragma mark --UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    if ([self.mbBarDelegate respondsToSelector:@selector(MBBarSearchViewSendSearchText:)]) {
        [self.mbBarDelegate MBBarSearchViewSendSearchText:textField.text];
    }
    return YES;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([self.mbBarDelegate respondsToSelector:@selector(MBBarSearchViewSendSearchText:)]) {
        
        NSString *name = [textField.text stringByAppendingString:string];
        [self.mbBarDelegate MBBarSearchViewSendSearchText:name];
    }
   
    return  YES;
}


@end
